import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Login from '../views/Login.vue'
import Resgister from '../views/Resgister.vue'
import User from '../views/User.vue'
import Edit from '../views/Edit.vue'
import MyFollow from '../views/MyFollow.vue'
import MyComments from '../views/MyComments.vue'
import MyStar from '../views/MyStar.vue'
import Home from '../views/Home.vue'
import Detail from '../views/Detail.vue'
const router = new VueRouter({
  routes: [
    { path: '/register', component: Resgister, name: 'register' },
    { path: '/myfollow', component: MyFollow, name: 'myfollow' },
    { path: '/login', component: Login, name: 'login' },
    { path: '/user', component: User, name: 'user' },
    { path: '/mycomments', component: MyComments, name: 'mycomments' },
    { path: '/mystar', component: MyStar, name: 'mystar' },
    { path: '/edit', component: Edit, name: 'edit' },
    { path: '/home', component: Home, name: 'home' },
    { path: '/detail/:id', component: Detail, name: 'detail' },
    { path: '/', redirect: '/login' }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path == '/user') {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
